{extend name="property/public/insidePageBase" /}
{block name="title"}智能电表 列表页面{/block}
{block name="body"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-form-pane" action="">
            <div class="layui-inline">
                <label class="layui-form-label">按物业筛选</label>
                <div class="layui-input-inline" style="width: 128px;">
                    <select name="property_id"  lay-search>
                        <option value="">--请选择--</option>
                        {foreach $propertyData as $k=>$v}
                        <option value="{$v['id']}">{$v['name']}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">按类型筛选</label>
                <div class="layui-input-inline" style="width: 128px;">
                    <select name="energy_type"  lay-search>
                        <option value="">--请选择--</option>
                        {foreach $energyTypeData as $k=>$v}
                        <option value="{$k}">{$v}</option>
                        {/foreach}
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label required">关键词搜索</label>
                <div class="layui-input-inline"  style="width: 158px;">
                    <input type="text" name="keywords" autocomplete="off" class="layui-input" placeholder="请输入编号/呢称信息">
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label">绑定状态</label>
                <div class="layui-input-inline" style="width: 100px;">
                    <select name="is_bind" lay-filter="aihao">
                        <option value="">-请选择-</option>
                        <option value="1">己绑定</option>
                        <option value="2">未绑定</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
            </div>
        </form>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                {if $addAuth}<button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add"> 添加 </button>{/if}
                {if $delAuth}<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete"> 删除 </button>{/if}
            </div>
        </script>
        <table class="layui-hide" id="electricMeterList" lay-filter="electricMeterList"></table>
        <script type="text/html" id="tableBar">
            <div class="layui-btn-group">
                {if $editAuth} <a class="layui-btn layui-btn-sm  layui-btn-normal" lay-event="edit">编辑</a> {/if}
                {if $detailAuth} <a class="layui-btn layui-btn-sm" lay-event="details">详情</a> {/if}
                {if $delAuth} <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a> {/if}
            </div>
        </script>
        <script type="text/html" id="on-off-tpl">
            {{# if(d.on_off === 1) { }}
            <span style="color: darkgreen;">通电中</span>
            {{# } }}
            {{# if(d.on_off === 2) { }}
            <span style="color: red;">断电中</span>
            {{# } }}
        </script>
        <script type="text/html" id="plan_type_tpl">
            {{# if(d.plan_type === 1) { }}<span class="layui-badge layui-bg-blue">后付费</span>{{# } }}
            {{# if(d.plan_type === 2) { }}<span class="layui-badge layui-bg-cyan">系统预付费</span>{{# } }}
            {{# if(d.plan_type === 3) { }}<span class="layui-badge layui-bg-red">离线预付费</span>{{# } }}

            {{# if(d.energy_type === 11) { }}<span class="layui-badge layui-bg-blue">电表</span>{{# } }}
            {{# if(d.energy_type === 12) { }}<span class="layui-badge layui-bg-cyan">冷水表</span> {{# } }}
            {{# if(d.energy_type === 13) { }}<span class="layui-badge layui-bg-red">热水表</span> {{# } }}
            {{# if(d.energy_type === 14) { }}<span class="layui-badge layui-bg-purple">燃气表</span> {{# } }}
        </script>
        <script type="text/html" id="status_tpl">
            {{# if(d.status === 1) { }}<span style="color: darkgreen;">在线</span>{{# } }}
            {{# if(d.status === 2) { }}<span style="color: red;">己离线</span> {{# } }}
        </script>
        <script type="text/html" id="wit-type-tpl">
            {{# if(d.wit_type === 1) { }}
               <span class="layui-badge layui-bg-red">智能</span>
            {{# } }}
            {{# if(d.wit_type === 2) { }}
            <span  class="layui-badge layui-bg-green">普通</span>
            {{# } }}
            {{d.energy_no}}
            {{# if(d.is_bind === 1) { }}<span class="layui-badge layui-bg-blue">绑</span>{{# } }}
            {{# if(d.is_public === 1) { }}<span class="layui-badge layui-bg-cyan">公</span>{{# } }}
        </script>
    </div>
</div>
{/block}
{block name="js"}
{__block__}
    <script>
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery, form = layui.form, table = layui.table;
            var tableId = 'electricMeterList';
            table.render({
                elem: '#'+tableId,
                id:tableId,
                url: '{:url("propertyEnergyMeterList")}',
                toolbar: '#toolbar',
                cols: [[
                    {type: "checkbox", width: 50, rowspan: 2},
                    {field: 'energy_no', minWidth: 228, title: '设备编号', sort: true, rowspan: 2, templet:'#wit-type-tpl'},
                    {field: 'energy_name', width: 198, title: '安装地址', rowspan: 2, sort: true},
                    {field: 'plan_type', width: 158, title: '设备类型', templet:'#plan_type_tpl', sort: true, rowspan: 2},
                    {field: 'status', width: 80, title: '状态', templet:'#status_tpl', sort: true},
                    {field: 'on_off', width: 80, title: '通断', templet:'#on-off-tpl', sort: true, rowspan: 2},
                    {field: 'beilv', width: 80, title: '倍率',  sort: true, rowspan: 2},
                    {field: 'read_z', width: 108, title: '最新读数', sort: true},
                    {field: 'read_time', width: 168,title: '抄表时间', sort: true, rowspan: 2, templet:(d) => {
                        if(parseInt(d.read_time) > 0) {
                            return d.read_time
                        }
                        return '未抄表'
                        }},

                    {field: 'property_name', width: 168, title: '物业信息', sort: true},
                    {field: 'plan_name', width: 168, title: '收费方案', sort: true},
                    {field: 'expire_time', width: 108, title: '过期时间', sort: true}
                    {if $editAuth || $delAuth || $unbindAuth || $readAuth}
                    ,{title: '操作', width: 168, toolbar: '#tableBar', rowspan: 2, fixed:'right'}
                    {/if}
                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                height: 'full-80'
            });
            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {table.reload(tableId, {page: { curr: 1  }, where: data.field}, 'data'); return false; });
            table.on('rowDouble('+tableId+')', function(obj){
                var data = obj.data;
                var url = '{:url("propertyEnergyMeterDetail")}?energy_id='+data.id;
                parent.layer.open({
                    title: data.energy_no +'  电表详情', type: 2, shade: 0.2, maxmin:true, shadeClose:true, move:false,area: ['1000px', '100%'],offset:  'rt', anim: 2, content: url , btn: false
                });
            });
            /**
             * toolbar监听事件
             */
            table.on('toolbar('+tableId+')', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var url = '{:url("propertyEnergyMeterAdd")}';
                    var index = layer.open({title: '添加能源设备', type: 2, shade: 0.2,maxmin:true, area: ['800px', '500px'], content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                            iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                                var field = data.field; //获取提交的字段
                                var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                                $.post(url, field, function (res) {
                                    layer.close(loadAdd);
                                    if (res.code === 0) {
                                        table.reload(tableId); //数据刷新
                                        layer.close(index); //关闭弹层
                                        layer.msg(res.msg, {icon: 6, offset: '15px'});
                                    } else {
                                        layer.msg(res.msg, {icon: 5, offset: '15px'});
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus(obj.config.id),data = checkStatus.data;
                    var data = checkStatus.data, id = [];
                    for (elem in data) {
                        id.push(data[elem].id);
                    }
                    if (id.length === 0) {
                        return layer.msg('请选择要删除的数据');
                    }
                    var url = '{:url("propertyEnergyMeterDel")}';
                    layer.confirm('己绑定设备会过滤删除功能，如有绑定请先移除绑定再删除，删除后不可恢复！  是否确认?', {icon: 3}, function () {
                        var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                        $.post(url, {ids: id}, function (res) {
                            layer.close(loadAdd);
                            if (res.code === 0) {
                                table.reload(tableId); //数据刷新
                                layer.msg(res.msg, {icon: 6, offset: '15px'});
                            } else {
                                layer.msg(res.msg, {icon: 5, offset: '15px'});
                            }
                        });
                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox('+tableId+')', function (obj) {
                console.log(obj)
            });
            table.on('tool('+tableId+')', function (obj) {
                var data = obj.data;
                if (obj.event === 'edit') {
                    var url = '{:url("propertyEnergyMeterEdit")}?id='+data.id;
                    var index = layer.open({
                        title: '编辑能源设备信息', type: 2,  shade: 0.2, maxmin:true, area: ['800px', '500px'],content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                            iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                                var field = data.field; //获取提交的字段
                                var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                                $.post(url, field, function (res) {
                                    layer.close(loadAdd);
                                    if (res.code === 0) {
                                        table.reload(tableId); //数据刷新
                                        layer.close(index); //关闭弹层
                                        layer.msg(res.msg, {icon: 6, offset: '15px'});
                                    } else {
                                        layer.msg(res.msg, {icon: 5, offset: '15px'});
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'details') {
                    var url = '{:url("propertyEnergyMeterDetail")}?energy_id='+data.id;
                    var index = parent.layer.open({title: data.energy_no +'  设备详情', type: 2, shade: 0.2, maxmin:true, shadeClose:true, move:false,area: ['1000px', '100%'],offset:  'rt', anim: 2, content: url , btn:false });
                    return false;
                } else if (obj.event === 'delete') {
                    var url = '{:url("propertyEnergyMeterDel")}';
                    layer.confirm('删除后不可恢复！  是否确认? ', {icon: 3}, function () {
                        var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                        $.post(url, {ids: [data.id]}, function (res) {
                            layer.close(loadAdd);
                            if (res.code === 0) {
                                table.reload(tableId); //数据刷新
                                layer.msg(res.msg, {icon: 6, offset: '15px'});
                            } else {
                                layer.msg(res.msg, {icon: 5, offset: '15px'});
                            }
                        });
                    });
                }
            });
        });
    </script>
{/block}